package work.utils;

import org.apache.poi.ss.usermodel.*;

import java.io.File;
import java.io.FileInputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class CommonExcelUtils {

    public static List<Map<String, String>> e2m(File file,String sheetName,int indexLine) {

        List<Map<String, String>> list = new ArrayList<>();
        try {

            Workbook sheets = WorkbookFactory.create(new FileInputStream(file));

            Sheet rows = sheets.getSheet(sheetName);


            //首先找到第二行
            Row index = rows.getRow(indexLine);//这一行为所银行

            short lastCellNum = index.getLastCellNum();
            String names[] = new String[lastCellNum];

            for (int i = 0; i < lastCellNum; i++) {
                names[i] = index.getCell(i).getStringCellValue();
            }

            //递归直到最后一行
            for (int i = indexLine + 1; i < rows.getLastRowNum() + 1; i++) {//

                //找到行
                Row row = rows.getRow(i);

                Map<String, String> map = new HashMap<>();

                for (int j = 0; j < names.length; j++) {

                    String name = names[j];
                    Cell cell = row.getCell(j);
                    try {

                        map.put(name, cell.toString());
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }

                //添加进list
                list.add(map);

            }

        } catch (Exception e) {
            e.printStackTrace();
        }
        return list;

    }

}
